<?php
/*

Class:     CTableGenerator
Version:   0.01
Author:    Krivosheya V.V. (c) May 2004

Available options (allow combine: "begin|end|header|td_class=1|td_style=red"):
- begin
- end
- header
- h_center
- td_class=<key>
- td_style=<key>
*/

class CTableGenerator2 {
	/* ----- Public Properties ----- */
	var $td_class = array("default" => "");
	var $td_style = array("default" => "");
	
	/* ----- Private Properties ----- */
	var $tab_padding_str	= "";
	var $cell_padding = "";
	var $tr_height	= "";
	var $row_is_odd	= false;
	var $row_bg_color0	= " bgcolor=\"#D3DCE3\"";
	var $row_bg_color1	= "";
	var $row_bg_color2	= "";
	
	function CTableGenerator2($tab_padding, $is_bgcolors, $cell_padding="5", $tr_height="")
	{
		// make level string
		$this->tab_padding_str = "";
		for ($i=0; $i<$tab_padding; $i++)
			$this->tab_padding_str .= "\t";
		// init chessboard
		$this->row_is_odd = false; 	// true - determine odd row
		if ($is_bgcolors) {
			$this->row_bg_color1 = " bgcolor=\"#DDDDDD\"";
			$this->row_bg_color2 = " bgcolor=\"#CCCCCC\"";
		}
		else {
			$this->row_bg_color1 = "";
			$this->row_bg_color2 = "";
		}
		//
		$this->cell_padding = $cell_padding;
		//
		$this->tr_height = "";
		if ($tr_height)	$this->tr_height = " height=\"$tr_height\"";
	}

	function PrintHeader()
	{
		echo $this->tab_padding_str."<!-- ----- BEGIN TABLE ----- -->\n";
		echo $this->tab_padding_str."<table border=0 cellspacing=1 cellpadding=0 style=\"padding: ".$this->cell_padding."; padding-right: 10\">\n";
	}
	
	function Cell($text="", $options="") 
	{
		// switch row color and print <TR> tag
		if ($this->GetOption($options, "begin")) 
		{
			if ($this->GetOption($options, "header")) {
				$row_bg_color = $this->row_bg_color0;
			} else {
				// switch row (odd/even)
				$this->row_is_odd = !$this->row_is_odd;
				if ($this->row_is_odd)
					$row_bg_color = $this->row_bg_color1; 
				else 
					$row_bg_color = $this->row_bg_color2;
			}
	
			// print tag
			echo $this->tab_padding_str."<tr".$this->tr_height.$row_bg_color.">\n";
		}
		
		// print <TD>...</TD> tag
		$td_align="";
		if ($this->GetOption($options, "h_center"))
			$td_align=" align=\"center\"";
			
		if ($this->GetOption($options, "header"))
			$text = "<b>".$text."</b>";
			
		$td_style = $this->td_style["default"];
		if ($tmp = $this->GetOption($options, "td_style"))
			$td_style = $this->td_style[$tmp];
		if ($td_style) $td_style=" ".$td_style;
			
		$td_class = $this->td_class["default"];
		if ($tmp = $this->GetOption($options, "td_class"))
			$td_class = $this->td_class[$tmp];
		if ($td_class) $td_class=" ".$td_class;
			
		echo $this->tab_padding_str."\t<td".$td_align.$td_class.$td_style.">".$text."</td>\n";
		
		// print </TR> tag
		if ($this->GetOption($options, "end")) {
			echo $this->tab_padding_str."</tr>\n";
		}
	}
	
	function PrintFooter() 
	{
		echo $this->tab_padding_str."</table>\n";
		echo $this->tab_padding_str."<!-- -----  END TABLE  ----- -->\n";
	}
	
	function GetOption($op_list, $op_name)
	{
		$op_item = strtok($op_list,"|");
		while ($op_item) {
			list($name, $value, $tmp) = split("=", $op_item."=yes=tmp");
			if ($name == $op_name)
				return $value;
			$op_item = strtok ("|");
		}
		return false;
	}
}
?>